<?php

namespace ABonus\Bundle\ExtractBundle\Manager;

use Doctrine\ORM\EntityManager;
use ABonus\Bundle\ExtractBundle\Entity;
use ABonus\Bundle\ExtractBundle\Repository;

/**
 * Description of BlocManager
 *
 */
class SectorManager
{
    protected $repository;
    protected $em;
    protected $kernel;

    public function __construct(EntityManager $em, $entityClass, $kernel)
    {
        $this->em = $em;
        $this->repository = $em->getRepository($entityClass);
        $this->kernel = $kernel;
    }

    public function getRepository()
    {
        return $this->repository;
    }

    public function getSectorFromName($sectorName)
    {
        return $this->repository->findOneBy(array('name' => $sectorName));
    }

    public function saveSector($newSector){
        $oldSector = $this->repository->findOneBy(array('name' => $newSector->getName()));
        if($oldSector == null){
            $this->em->persist($newSector);
            $this->em->flush();
            return $newSector;
        }else{
            $oldSector->setGpsParkingLot($newSector->getGpsParkingLot());
            $oldSector->setName($newSector->getName());
            $oldSector->setBlocs($newSector->getBlocs());
            $oldSector->setNumberOf7a($newSector->getNumberOf7a());
            $this->em->flush();
            return $oldSector;
        }
    }
}
